package m.html.form;

import m.html.element.Button;
import m.html.element.Element;
import m.html.element.attr.ButtonType;
import m.html.element.other.IElement;
import m.system.util.StringUtil;

public class FormButton extends Button{
    public FormButton(ButtonType type,String title,String methodName){
        super(type);
        this.E(title);
        this.methodName=methodName;
        this.test="true";
    }
    private final String methodName;
    private String confirm;
    private String description;
    private String test;
    private IElement elseElement;
    public FormButton setConfirm(String confirm){
        this.confirm=confirm;
        return this;
    }
    /**
     * 设置描述，鼠标放上去现实
     */
    public FormButton setDescription(String description){
        this.description=description;
        return this;
    }

    /**
     * 表达式控制显示隐藏
     */
    public FormButton setTest(String test){
        this.test=test;
        return this;
    }

    /**
     * 设置test为false时展示元素
     */
    public FormButton setElse(IElement ele){
        this.elseElement=ele;
        return this;
    }

    /**
     * 表达式控制禁用
     */
    @Override
    public FormButton setDisabled(String disabled) {
        super.setDisabled(disabled);
        return this;
    }

    @Override
    public String toString() {
        String html=null;
        if(StringUtil.isSpace(this.confirm)){
            this.A("@click",methodName);
            this.A("v-if",this.test);
            html=super.toString();
        }else{
            Element tag=Element.tag("a-popconfirm").A("title",confirm)
                .A("@confirm",methodName);
            tag.A("v-if",this.test);
            tag.E(super.toString());
            html=tag.toString();
        }
        if(StringUtil.notSpace(this.description)){
            html=Element.tag("a-tooltip")
                .E(Element.tag("template").A("#title").E(this.description))
                .E(html).toString();
        }
        if(null!=this.elseElement) html+=this.elseElement.A("v-else").toString();
        return html;
    }
    public static FormButton def(String title, String methodName){
        return new FormButton(ButtonType.DEFAULT,title,methodName);
    }
    public static FormButton pri(String title,String methodName){
        return new FormButton(ButtonType.PRIMARY,title,methodName);
    }
    public static FormButton err(String title,String methodName){
        return new FormButton(ButtonType.ERROR,title,methodName);
    }
    public static FormButton link(String title,String methodName){
        return new FormButton(ButtonType.LINK,title,methodName);
    }
    public static FormButton gho(String title,String methodName){
        return new FormButton(ButtonType.GHOST,title,methodName);
    }
    public static FormButton ghoErr(String title,String methodName){
        return new FormButton(ButtonType.GHOST_ERROR,title,methodName);
    }
    public static FormButton linkErr(String title,String methodName){
        return new FormButton(ButtonType.LINK_ERROR,title,methodName);
    }
    public static FormButton succ(String title,String methodName){
        return new FormButton(ButtonType.SUCCESS,title,methodName);
    }
    public static FormButton warn(String title,String methodName){
        return new FormButton(ButtonType.WARN,title,methodName);
    }
}
